package handler

import (
	"github.com/gin-gonic/gin"
	"github.com/lab-online/internal/report/constant"
	"github.com/lab-online/internal/report/entity"
	"github.com/lab-online/pkg/auth"
	mw "github.com/lab-online/pkg/middleware"
	"github.com/lab-online/pkg/resp"
)

type getReportReqParams struct {
	ID uint `uri:"id" binding:"required,min=1" example:"1"` // 报告ID
}

var GetReportValidator = mw.NewValidator(func() *mw.ValidatorOptions {
	return &mw.ValidatorOptions{
		Params: &getReportReqParams{},
	}
})

type GetReportRespBody struct {
	resp.BaseRespBody
	Data reportInfoWithContent `json:"data" binding:"required"`
}

// SearchReport 搜索报告
//
//	@Summary				搜索报告
//	@Description.markdown	SearchReport
//	@Tags					report
//	@Accept					json
//	@Produce				json
//	@Security				bearer
//	@Param					Authorization	header		string				true	"`all`"
//	@Param					report			query		getReportReqParams	true	"报告信息"
//	@Success				200				{object}	GetReportRespBody
//	@Failure				400				{object}	resp.FailedRespBodyString
//	@Router					/v1/reports/:id	[get]
func (h *Handler) GetReport(c *gin.Context) {
	params := c.MustGet(mw.KeyParams).(*getReportReqParams)
	authEntity := c.MustGet(auth.KeyAuth).(auth.AuthEntity)

	reportEntity := entity.New(
		entity.WithID(params.ID),
	)

	if report, err := h.domain.GetReport(authEntity, reportEntity); err != nil {
		resp.ErrorHandler(c, err)
	} else {
		resp.Success(c, constant.CodeGetReportSuccess, &reportInfoWithContent{
			reportInfo: reportInfo{
				ID:           report.GetID(),
				ExperimentID: report.GetExperimentID(),
				ClassID:      report.GetClassID(),
				Student:      report.GetStudent(),
				Score:        report.GetScore(),
				Status:       report.GetStatus(),
			},
			Content: report.GetContent(),
		})
	}
}
